#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
using namespace std;

class Solution {
public:
    int tmp[50000];

    void mergesort(vector<int>& nums, int begin, int end, int& ret)
    {
        if (begin >= end) return;
        int mid = (begin + end) >> 1;
        mergesort(nums, begin, mid, ret);
        mergesort(nums, mid + 1, end, ret);
        int cur1 = begin, cur2 = mid + 1, i = begin;
        while (cur1 <= mid && cur2 <= end)
        {
            if (nums[cur1] / 2. > nums[cur2])
            {
                ret += end - cur2 + 1;
                ++cur1;
            }
            else ++cur2;
        }
        cur1 = begin; cur2 = mid + 1;
        while (cur1 <= mid && cur2 <= end)
        {
            if (nums[cur1] <= nums[cur2]) tmp[i++] = nums[cur2++];
            else tmp[i++] = nums[cur1++];
        }
        while (cur1 <= mid) tmp[i++] = nums[cur1++];
        while (cur2 <= end) tmp[i++] = nums[cur2++];
        for (int j = begin; j <= end; ++j) nums[j] = tmp[j];
        //    for(auto e:nums) cout<<e<<' ';
        //    cout<<endl;
    }
    int reversePairs(vector<int>& nums) {
        int ret = 0;
        mergesort(nums, 0, nums.size() - 1, ret);
        return ret;
    }
};